package com.niit.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;

import com.niit.dao.BookDao;
import com.niit.entity.Book;
import com.niit.utils.ConnectionUtil;

public class BookDaoImpl implements BookDao {
	
	@Override
	public Vector<Book> bookList(String bookName) {
		String sql;
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		
		Vector<Book> booklist = new Vector<>();
		sql = "select * from sys_books where 1=1 ";
		try {
			conn = ConnectionUtil.getConnection();
			if(bookName!=null&&!bookName.trim().equals("")){
				bookName="%"+bookName+"%";
				sql += "and book_name like ?";
			}
			psmt = conn.prepareStatement(sql);
			if(bookName!=null&&!bookName.trim().equals("")){
				psmt.setString(1, bookName);
			}
			
			rs = psmt.executeQuery();
			while(rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt("id"));
				book.setBookName(rs.getString("book_name"));
				book.setBookAuthor(rs.getString("book_author"));
				book.setBookPublisher(rs.getString("book_publisher"));
				book.setBookPrice(rs.getDouble("book_price"));
				book.setCreateTime(rs.getTimestamp("create_time"));
				booklist.add(book);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ConnectionUtil.close(conn, psmt, rs);
		}
		
		return booklist;
	}

	@Override
	public void delete(int id) {
		String sql;
		Connection conn = null;
		PreparedStatement psmt = null;
		Integer rs ;
		try {
			sql = "delete from sys_books where id = ? ";
			conn = ConnectionUtil.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, id);
			rs = psmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ConnectionUtil.close(conn, psmt, null);
		}
	}

	
	@Override
	public boolean insertBook(Book book) {
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "insert into sys_books(book_price,book_name,book_author,book_publisher,create_time)"
				+ "values (?,?,?,?,?) ";
		try {
			conn = ConnectionUtil.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setDouble(1, book.getBookPrice());
			psmt.setString(2, book.getBookName());
			psmt.setString(3, book.getBookAuthor());
			psmt.setString(4, book.getBookPublisher());
			psmt.setTimestamp(5, book.getCreateTime());
			psmt.executeUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ConnectionUtil.close(conn, psmt, rs);
		}
		return false;
	}

	@Override
	public void update(Book book) {
		String sql;
		Connection conn = null;
		PreparedStatement psmt = null;
		Integer rs ;
		try {
			sql = "update sys_books set book_price = ?,book_name=?,book_author=?,book_publisher=?,create_time=? where id = ?";
			conn = ConnectionUtil.getConnection();
			psmt = conn.prepareStatement(sql);
			psmt.setDouble(1, book.getBookPrice());
			psmt.setString(2, book.getBookName());
			psmt.setString(3, book.getBookAuthor());
			psmt.setString(4, book.getBookPublisher());
			psmt.setTimestamp(5, book.getCreateTime());
			psmt.setInt(6, book.getId());
			rs = psmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			ConnectionUtil.close(conn, psmt, null);
		}
	}

}
